const express = require('express');
const router = express.Router();
const { body } = require('express-validator');
const authController = require('../controllers/authController');
const { protect } = require('../middleware/auth');

// 验证规则
const loginValidation = [
  body('account').notEmpty().withMessage('请输入账号'),
  body('password').notEmpty().withMessage('请输入密码')
];

const phoneLoginValidation = [
  body('phone').matches(/^1[3-9]\d{9}$/).withMessage('请输入正确的手机号'),
  body('code').isLength({ min: 6, max: 6 }).withMessage('验证码长度不正确')
];

const registerValidation = [
  body('phone').matches(/^1[3-9]\d{9}$/).withMessage('请输入正确的手机号'),
  body('code').isLength({ min: 6, max: 6 }).withMessage('验证码长度不正确'),
  body('password').isLength({ min: 6 }).withMessage('密码长度不能少于6位')
];

// 路由
router.post('/login', loginValidation, authController.login);
router.post('/phone-login', phoneLoginValidation, authController.phoneLogin);
router.post('/send-code', authController.sendVerificationCode);
router.post('/register', registerValidation, authController.register);
router.post('/forgot-password', authController.forgotPassword);
router.post('/reset-password', authController.resetPassword);
router.post('/refresh-token', authController.refreshToken);

module.exports = router; 